package view.plugin
{
	//左上角消息栏
	
	import event.GlobalEvent;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextLineMetrics;
	import flash.utils.getDefinitionByName;
	
	import flash3.framework.Singleton;
	import flash3.net.MsgPacket;
	import flash3.net.NetFacade;
	
	import global.Resource;
	
	import model.*;
	
	import net.FrameGroup;
	import net.UserGroup;
	
	import util.GifPlayer;
	import util.RichTextArea;
	
	import vo.Message;
	import vo.UserChat;
	import vo.UserInfo;
	
	public class OutputPanel extends Sprite
	{
		private var panel:MovieClip = null;
		private var textArea:RichTextArea;
		
		public function OutputPanel():void
		{
			panel = Resource.i.getMovieClip("common", "ResPanel");
			textArea = new RichTextArea(panel.width-5,panel.height-5);
			textArea.x = 5;
			textArea.configXML = ChatFace.faceXML;
			textArea.textField.wordWrap=true;
			textArea.textField.multiline=true;
			addChild(panel);
			addChild(textArea);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoveFromStage);
			
			this.x = 5; this.y = 80;
		}
		private function onAddToStage(evt:Event):void
		{
//			var message:Message = new Message();
//			message.wMessageType = Message.SMT_REWARDINFO;
//			message.szContent = "ksldfjl看dsl房间开始的浪费空间30%";
//			systemMessage(message);
			NetFacade.i.acall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_CHAT,{call:onNotifyUserChat,forever:true});
			NetFacade.i.acall(FrameGroup.MDM_GF_FRAME,FrameGroup.SUB_GF_USER_CHAT,{call:onNotifyUserChat,forever:true});
			NetFacade.i.acall(FrameGroup.MDM_GF_FRAME,FrameGroup.SUB_GF_MESSAGE,{call:onNotifySystemMessage,forever:true});
		}
		private function onRemoveFromStage(evt:Event):void
		{
			NetFacade.i.rcall(UserGroup.MDM_GR_USER,UserGroup.SUB_GR_USER_CHAT);
			NetFacade.i.rcall(FrameGroup.MDM_GF_FRAME,FrameGroup.SUB_GF_USER_CHAT);
			NetFacade.i.rcall(FrameGroup.MDM_GF_FRAME,FrameGroup.SUB_GF_MESSAGE);
		}
		private function onNotifyUserChat(...parms):void
		{
			var message:UserChat = parms[0]._parm as UserChat;
			userMessage(message);
			var evt:GlobalEvent = new GlobalEvent(GlobalEvent.GLOBAL_USERCHAT);
			evt.data = message;
			dispatchEvent(evt);
		}
		private function onNotifySystemMessage(...parms):void
		{
			var message:Message = parms[0]._parm as Message;
			if(!message) return;
			systemMessage(message);
		}
		
		private function systemMessage(message:Message):void
		{
			if((message.wMessageType & Message.SMT_REWARDINFO) != 0)
			{
				outMessage("<b><font face='verdana' color='#a97fe8' size='12' leading='5' >[奖励]: </font> </b>"
					+"<font face='verdana' color='#7a5dcb' size='12' leading='5' >"+message.szContent+"</font>");
			}
			else
			{
				outMessage("<b><font face='verdana' color='#a97fe8' size='12' leading='5' >[系统]: </font> </b>"
					+"<font face='verdana' color='#7a5dcb' size='12' leading='5' >"+message.szContent+"</font>");
			}
		}
		
		private function userMessage(message:UserChat):void
		{
			var userInfo:UserInfo = UserInfoModel.i.value(message.dwSendUserID);
			if(!userInfo) return;
			outMessage("<b><font face='verdana' color='#a97fe8' size='12' leading='5'>[" +userInfo.szAccounts+ "]说："+"</font></b>"
				+"<font face='verdana' color='#7a5dcb' size='12' leading='5' >"+message.szChatMessage+"</font>");
		}
		
		public function outMessage(message:String):void
		{
			richTextArea.appendRichText(message);
			richTextArea.textField.scrollV = richTextArea.textField.maxScrollV;
		}
		
		public function show(visible:Boolean):void
		{
			if(visible)
				ChessLobby.chat.addChild(this);
			else if(parent)
				parent.removeChild(this);
		}
		
		public function get richTextArea():RichTextArea
		{
			return textArea;
		}
		
		public function clear():void
		{
			textArea.clear();
		}
		
		static public function get i():OutputPanel
		{
			return Singleton.getInstanceOrCreate(OutputPanel) as OutputPanel;
		}
	}
	
}